//index.js
const app = getApp()
var that;
var util = require('../../utils/util.js');

Page({
  data: {
    userId:"",
    DataList:[]
  },
  
  onLoad: function () {
    that = this;

    // 判断登陆状态
    if (app.globalData.checkLogin) {
      console.log('判断onLaunch是否执行完毕1')
      if (app.globalData.loginUserObj != null) {
        that.setData({
          userId: app.globalData.loginUserObj.id
        })
        //触发获取会议列表
        that.getMeetingFun();
        
        // 如果用户未选择角色，弹出选择弹层
        if (app.globalData.loginUserObj.hasOwnProperty("role") && app.globalData.loginUserObj.role === null) {
          that.openMaskFun();
        }
      }
    } else {
      app.checkLoginReadyCallback = res => {
        console.log('判断onLaunch是否执行完毕2', res)
        if (app.globalData.loginUserObj != null) {
          that.setData({
            userId: app.globalData.loginUserObj.id
          })
          //触发获取会议列表
          that.getMeetingFun();

          // 如果用户未选择角色，弹出选择弹层
          if (app.globalData.loginUserObj.hasOwnProperty("role") && app.globalData.loginUserObj.role === null) {
            that.openMaskFun();
          }
        }
      };
    }
  },

  //获取会议列表
  getMeetingFun:function(){
      that = this;
      wx.request({
        url: app.http + 'hysMeeting/myMeetingList',
        data: { 
          userId: that.data.userId
        },
        method: 'get',
        header: {
          'content-type': 'application/json', // 默认值
          'APP-User-Token': app.globalData.tokenMap.access_token
        },
        success: function (res) {
          console.log("首页列表suc返回", res)
          if (res.data.code == 200) {
              let dataArry = res.data.data
              dataArry.forEach(function(fn){
                fn.riqi = that.riqi(fn.riqi)
                fn.Time =  fn.riqi +
                ' '+ fn.startTime.substr(11,5) + '-' + fn.endTime.substr(11,5)
              })
              that.setData({
                DataList: dataArry
              })
          }
          else if (res.data.code == 100101) {
            // token无效，获取新的token
            var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
              function (res) {
                if (res.data.code == 200) {
                  that.getMeetingFun();
                }
                else if (res.data.code == 400) {
                  var wxLoginFun = util.wxLoginFun().then(
                    function (res2) {
                      if (res2.data.code == 200) {
                        that.getMeetingFun();
                      }
                    }
                  )
                }
              }
            ).catch(
              function (err) {
                console.log("获取新的token失败返回", err)
              }
            )
          }
        },
        fail: function (err) {
          console.log("fail返回", err)
        }
      })
  },
  // 昨天今天明天，日期计算。
  riqi(time){
    //昨天的时间
    var day1 = new Date();
    day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
    var s1 = day1.getFullYear() + "-" + ((day1.getMonth() + 1) < 10 ? 
      '0' + (day1.getMonth() + 1) : (day1.getMonth() + 1)) + "-" + (day1.getDate() < 10 ? 
      '0'+ day1.getDate() : day1.getDate());
    //今天的时间
    var day2 = new Date();
    day2.setTime(day2.getTime());
    var s2 = day2.getFullYear() + "-" + ((day2.getMonth() + 1) < 10 ?
      '0' + (day2.getMonth() + 1) : (day2.getMonth() + 1)) + "-" + (day2.getDate() < 10 ?
      '0' + day2.getDate() : day2.getDate());
    //明天的时间
    var day3 = new Date();
    day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
    var s3 = day3.getFullYear() + "-" + ((day3.getMonth() + 1) < 10 ?
      '0' + (day3.getMonth() + 1) : (day3.getMonth() + 1)) + "-" + (day3.getDate() < 10 ?
      '0' + day3.getDate() : day3.getDate());

    return   time == s1 ? '昨天' : time == s2 ? '今天' : time == s3 ? '明天' : time
  },

  // 邀请码
  toMmeetingCodeFun: function(e) {
    wx.navigateTo({
      url: '/pages/meetingDetailCode/meetingDetailCode',
    })
  },

  // 管理
  toMmeetingManageFun: function (e) {
    wx.navigateTo({
      url: '/pages/meetingManage/meetingManage',
    })
  },

  // 查看
  toMeetingDetailFun: function (e) {
    wx.navigateTo({
      url: '/pages/meetingDetail/meetingDetail',
    })
  },

  //打开选择身份弹出层
  openMaskFun: function () {
    //获得maskChooseType组件
    var kefuCom = this.selectComponent("#kefuMaskId");
    kefuCom.setData({
      isMaskTan: true
    })
  },

  
})
